package com.svn.util;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XMLReader {
	private String path = "/conf/svn.xml";
	private static XMLReader reader;
	private Map<String,String> elements;
	
	public static XMLReader getInstance(){
		if(reader == null)
			reader = new XMLReader();
		return reader;
	}
	
	public XMLReader(){
		elements = new HashMap<String,String>();
		InputStream resourceAsStream = getClass().getResourceAsStream(path);
		try {
			Document read = new SAXReader().read(resourceAsStream);
			Element rootElement = read.getRootElement();
			List elements2 = rootElement.elements();
			for (Iterator iterator = elements2.iterator(); iterator.hasNext();) {
				Element object = (Element) iterator.next();
				elements.put(object.attributeValue("name"), object.getTextTrim());
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	public String getElementValueByName(String name){
		return elements.get(name);
	}
	
}
